import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { reqNaveMenu,reqMainPage,reqElderList } from "@/api/index";

export const useCounterStore = defineStore('counter', ()=>{
  const menuList = ref([])
  const lv2MenuList = ref([])
  const indexLv0 = ref(0)
  const indexLv1 = ref(0)
  const indexLv2 = ref(0)
  const mainPageData = ref([])
  const elderListData = ref([])
  
  function getNavMenuList(index:number){
    reqNaveMenu().then((res)=>{
      menuList.value = res.data;
      lv2MenuList.value = menuList.value[index]['children'];
    })
  }
  function getlv2MenuList(index:number){
    lv2MenuList.value = menuList.value[index]['children'];
  }
  function changeLv2Index(index0:number,index1:number){
    indexLv0.value = index0;
    indexLv1.value = index1;
  }
  function getMainPage(){
    reqMainPage().then((res)=>{
      mainPageData.value = res.data;
    })
  }
  function getElderList(){
    reqElderList().then((res)=>{
      elderListData.value = res.data;
    })
  }

  const navMenuList = computed(()=>{
    return menuList.value;
  })
  const navLv2MenuList = computed(()=>{
    return lv2MenuList.value;
  })
  const lv1Children = computed(()=>{
    return navLv2MenuList.value[indexLv1.value];
  })

  return {
    menuList,
    lv2MenuList,
    mainPageData,
    getNavMenuList,
    getlv2MenuList,
    getMainPage,
    navMenuList,
    navLv2MenuList,
    lv1Children,
    changeLv2Index,
    elderListData,
    getElderList,
  }
  
})
